#pragma once

#include "Server.h"
#include "SvrMgr.h"
#include "Client.h"
#include "ClntList.h"

typedef CClntList<CClient> CClientList;
typedef CClientList::ListNode ClientNode;

class CWorkSvr : public CServer
{
public:
    CWorkSvr();
    virtual ~CWorkSvr();

public:
    bool Init(int *fds);
    // bool Start();
    bool Stop();
    bool Destroy();

private:
    bool ErrorEvent(epoll_event &ee);
    bool InputEvent(epoll_event &ee);
    bool OutputEvent(epoll_event &ee);
    bool IpcCmdEvent(IpcPkgBuf &pbRecv);

    // IPC收到数据后发给客户端
    bool IpcSendData(IpcPkgBuf &pbRecv);

    // IPC收到数据后给客户端处理
    bool IpcDataHandle(IpcPkgBuf &pbRecv);

public:
    unsigned int GetClientCount() { return m_lstClnt.GetSize(); }

private:
    bool AcceptEvent();
    bool AcceptHandle();
    bool RecvHandle(CClient *pClnt);
    bool SendHandle(CClient *pClnt);

private:
    CClient *CreateClient(int ifd);
    void DestroyClient(CClient *pClnt);

private:
    int m_ilfd;
    SSL_CTX *m_ctxSvr;
    CPkgBufMgr *m_pbm; // 数据包缓冲管理
    CClientList m_lstClnt;
};
